/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package caa.struts.compensation.action;

import caa.struts.compensation.form.ManageCompensationClassForm;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import th.com.entity.CourseEntity;
import th.com.entity.LocationEntity;
import th.com.entity.MakeupclassEntity;
import th.com.entity.ScheduleEntity;
import th.com.entity.SemesterEntity;
import th.com.entity.UserEntity;
import th.com.ku.CAAService;

/**
 *
 * @author omega
 */
public class CompensationManageAction extends DispatchAction {

    public ActionForward doAddMakeupClass(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageCompensationClassForm compensationClassForm = (ManageCompensationClassForm) form;
        
        ArrayList<LocationEntity> locationList = CAAService.selectLocation();
        compensationClassForm.setListLocation(locationList);
        
        compensationClassForm.setScheduleEntity(compensationClassForm.getListSchedule().get(compensationClassForm.getRowId()));
        
        return mapping.findForward("addmakeupclass.success");
    }
    
    public ActionForward doDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageCompensationClassForm compensationClassForm = (ManageCompensationClassForm) form;
        String userId = ((UserEntity)request.getSession().getAttribute("isUsername")).getUseId();
        
        compensationClassForm.getMakeupclassEntitys()[compensationClassForm.getRowId()].setMarcModifyBy(userId);
        compensationClassForm.getMakeupclassEntitys()[compensationClassForm.getRowId()].setLocId(0);
        compensationClassForm.getMakeupclassEntitys()[compensationClassForm.getRowId()].setMarcCompenDate(null);
        compensationClassForm.getMakeupclassEntitys()[compensationClassForm.getRowId()].setMarcCompenBeginTime(null);
        compensationClassForm.getMakeupclassEntitys()[compensationClassForm.getRowId()].setMarcCompenEndTime(null);
        compensationClassForm.getMakeupclassEntitys()[compensationClassForm.getRowId()].setMarcAmount(0.0);
        
        CAAService.updateMakeupClass(compensationClassForm.getMakeupclassEntitys()[compensationClassForm.getRowId()]);
        
        return mapping.findForward("managemakeupclass.success");
    }
    
    public ActionForward doUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageCompensationClassForm compensationClassForm = (ManageCompensationClassForm) form;
        String userId = ((UserEntity)request.getSession().getAttribute("isUsername")).getUseId();
        
        compensationClassForm.getMakeupclassEntitys()[compensationClassForm.getRowId()].setMarcModifyBy(userId);
        CAAService.updateMakeupClass(compensationClassForm.getMakeupclassEntitys()[compensationClassForm.getRowId()]);
        
        return mapping.findForward("managemakeupclass.success");
    }
    
    public ActionForward doBack(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        return mapping.findForward("managemakeupclass.success");
    }
    
    public ActionForward doMakeupClassList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageCompensationClassForm compensationClassForm = (ManageCompensationClassForm) form;

        ScheduleEntity scheduleEntity = new ScheduleEntity();
        scheduleEntity.setCouId(compensationClassForm.getCouId());
        compensationClassForm.setListSchedule(CAAService.findSchedule(scheduleEntity));
        compensationClassForm.setMakeupclassEntitys(new MakeupclassEntity[compensationClassForm.getListSchedule().size()]);
        
        CourseEntity courseEntity = new CourseEntity();
        courseEntity.setCouId(compensationClassForm.getCouId());
        courseEntity = CAAService.findCourse(courseEntity);
        
        for(int i = 0;i < compensationClassForm.getMakeupclassEntitys().length;i++) {
            compensationClassForm.getMakeupclassEntitys()[i] = new MakeupclassEntity();
            int marcId = compensationClassForm.getListSchedule().get(i).getMarcId();
            if(marcId > 0) {
                MakeupclassEntity makeupclassEntity = CAAService.findMakeupClass(marcId);
                compensationClassForm.getMakeupclassEntitys()[i] = makeupclassEntity;
                compensationClassForm.getMakeupclassEntitys()[i].setMarcAmount(courseEntity.getCouPricePerTime());
            }
            
        }
        
        return mapping.findForward("managemakeupclass.success");
    }
    
    public ActionForward doMakeupClass(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageCompensationClassForm compensationClassForm = (ManageCompensationClassForm) form;
        
        ArrayList<SemesterEntity> list = CAAService.findConfirmClass(compensationClassForm.getKwKeyWord());
        compensationClassForm.setListSearch(list);

        return mapping.findForward("makeupclass.success");
    }   
    
    public ActionForward doMakeupClassT(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageCompensationClassForm compensationClassForm = (ManageCompensationClassForm) form;
        
        UserEntity userEntity = (UserEntity)request.getSession().getAttribute("isUsername");
        
        ArrayList<SemesterEntity> list = CAAService.findConfirmClassT(userEntity.getUseId());
        compensationClassForm.setListSearch(list);

        return mapping.findForward("makeupclass.success");
    }    
    
    
    
}
